/**
 * Created by home on 2017/12/18.
 */
var app = angular.module('userFirst', []);

app.controller('userFirstController', function ($scope, $http, $filter) {

    var jobnum = null;
    $scope.user = null;
    //获取用户信息
    $scope.getuser = function () {
        $http({
            method: 'post',
            url: 'http://119.29.0.217:8080/sctu_it_operation_platform/getUserInfo',
            data: {"userAccount": "1506101032"}
        }).then(function (response) {
            console.log(response);
            $scope.user = response.data.data;
        }, function errorCallback(response) {
            // 请求失败执行代码
            jError("获取用户信息失败！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });
        });
    };

    $scope.getuser();
    $scope.test = function () {
        //首先判断工单有没有正在进行的，如果没有才可以再次提交工单，如果有，就不能提交新的工单
        var iscom =0;
        var iscomp = 0;
        //$http({
        //    method: 'get',
        //    url: 'http://localhost:8080/userWorkOrders/' + $scope.user.userAccount
        //}).then(function successCallback(response) {
        //    console.log(response);
        //    $http({
        //        method: 'get',
        //        url: 'http://localhost:8080/findRepairOrder/' + $scope.user.userAccount
        //    }).then(
        //        function successback(data){
        //            jobnum = data.data.data.jobnumber;
        //        }
        //    );
        //});

        console.log("jobnumber,"+jobnum);
        //$http({
        //    method: 'get',
        //    url: 'http://localhost:8080/isComment/'+jobnum
        //}).then(
        //    function successback(data){
        //        console.log(data);
        //        if(data.data!=null||data.data!=""){
        //            iscom =  1;
        //        }else{
        //            iscom =  0;
        //        }
        //        //如果已经评论过，就不可以再评论
        //    },function errorback(){}
        //);
        //$http({
        //    method:'get',
        //    url:'/isComplaint/'+jobnum
        //}).then(
        //    function successback(data){
        //        if(data!=null){
        //            iscomp =  1;
        //        }
        //
        //        //如果已经投诉过，就不可以再投诉
        //    },function errorback(){}
        //);
        //if(iscom==1||iscomp==1){
        //    console.log("是否评价"+iscom+","+iscomp);
            //进行判断
//如果没有未评价或者未投诉的工单就可以再次提交新的工单
            //拿到故障
            var details1 = $scope.selected;
            var addresss = document.getElementById("useraddress").value;

            if(details1!=null&&details1!=""&&details1!=undefined){
                var str = details1.split(",");
                if(str[0]==null||addresss==null||str[0]==""||addresss==""||str[0]==undefined||addresss==undefined){
                    alert("请将信息填写完整！");
                }else{
                    $http({
                        method: 'post',
                        url: 'http://localhost:8080/addWorkOrder',
                        data: {
                            //"details": details1,
                            "address": addresss,
                            "type": "1",
                            "userAccount": $scope.user.userAccount
                        }
                    }).then(function successCallback(response) {
                        console.log(response.data.data);
                        jSuccess("创建工单成功，系统正在为您派单...",{
                            VerticalPosition: 'center',
                            HorizontalPosition: 'center'
                        });
                        //创建工单成功后，就会分配运维人员，此时，系统就给运维人员发送短信进行提示
                        $scope.operators = {
                            phone:response.data.data.userPhone,
                            content:"亲爱的运维人员"+response.data.data.userName+"您接到一个工单，请尽快处理！",
                            username:null,//短信接口用户名
                            password:null//短信接口密码
                        };
                        $http({
                            method:"get",
                            url:"http://172.16.10.80:8085/SendMessage.aspx?phone=13666140399&content="+$scope.operators.content+"&username=lyityw&password=ityw_2017",
                            data:$scope.operators,
                            header:'Access-Control-Allow-Headers:x-requested-with,content-type'
                        }).then(
                            function successback(data){
                                jSuccess("派单成功！请保持电话开机，我们的运维人员稍后将联系您！",{
                                    VerticalPosition: 'center',
                                    HorizontalPosition: 'center'
                                })
                            },function errorback(data){
                                jSuccess("派单成功！请保持电话开机，我们的运维人员稍后将联系您！",{
                                    VerticalPosition: 'center',
                                    HorizontalPosition: 'center'
                                })
                            }
                        );
                    }, function errorCallback() {
                        jError("创建工单失败！",{
                            VerticalPosition: 'center',
                            HorizontalPosition: 'center'
                        });
                    });
                }

            }else{
                alert("请将信息填写完整！");
            }
        //}else{
        //    console.log("是否评价"+iscom+","+iscomp);
        //    alert("有个工单未评价哦，暂时不能提交新的工单呢~");
        //}

    };
//查看正在处理的工单的详情
    $scope.baoxiujingdu = function () {
        if ($scope.user != null) {
            $http({
                method: 'get',
                url: 'http://localhost:8080/userWorkOrders/' + $scope.user.userAccount

            }).then(function successCallback(response) {
                console.log(response);

                $http({
                    method: 'get',
                    url: 'http://localhost:8080/findRepairOrder/' + $scope.user.userAccount
                }).then(function successCallback(response) {
                    console.log(response);
                    $scope.jobnumber = response.data.data.jobnumber;
                    jobnum = response.data.data.jobnumber;
                    $scope.details = response.data.data.details;
                    $scope.userAccount = response.data.data.userAccount;
                    $scope.userPhone = response.data.data.userPhone;
                    $scope.userName = response.data.data.userName;
                    $scope.uploadTime = response.data.data.uploadTime;
                    $scope.dt3 = $filter("date")($scope.uploadTime, "yyyy-MM-dd HH:mm:ss");

                    var now = new Date();
                    var dt2 = now.getTime() - new Date($scope.dt3).getTime();
                    //计算出相差天数
                    var days = Math.floor(dt2 / (24 * 3600 * 1000));//计算出小时数

                    var leave1 = dt2 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
                    var hours = Math.floor(leave1 / (3600 * 1000));//计算相差分钟数
                    var leave2 = leave1 % (3600 * 1000);      //计算小时数后剩余的毫秒数
                    var minutes = Math.floor(leave2 / (60 * 1000));//计算相差秒数
                    var leave3 = leave2 % (60 * 1000);  //计算分钟数后剩余的毫秒数
                    var seconds = Math.round(leave3 / 1000);
                    $scope.dt1 = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";


                }, function errorCallback(response) {

                });
            }, function errorCallback(response) {
                jError("查看工单详情失败！",{
                    VerticalPosition: 'center',
                    HorizontalPosition: 'center'
                });
            });
        }


    };
    $scope.baoxiujingdu();

//用户对工单进行评价
    $scope.userRemark = function (b, a) {

        //用户点击评论之后，向后台发送请求，如果已经评论过，就不可以再评论，如果没有评论过就可以再评论
        $http({
            method: 'get',
            url: 'http://localhost:8080/isComment/'+b
        }).then(
            function successback(data){
                var  isc =  data.data.data.iscomment;
                //如果已经评论过，就不可以再评论
                if(isc==1){
                    alert("您已经评论过了哦~");
                }else {
                    //如果没有评论过，才可以评论
                    $http({
                        method: 'post',
                        url: 'http://localhost:8080/workOrders/review',
                        data: {"jobnumber": b, "commentStar": a}
                    }).then(function successCallback(response) {
                        $scope.remark = response.data.data;
                        console.log(response);
                        jSuccess("评价成功！",{
                            VerticalPosition: 'center',
                            HorizontalPosition: 'center'
                        });

                    }, function errorCallback(response) {
                        jError("评价失败！",{
                            VerticalPosition: 'center',
                            HorizontalPosition: 'center'
                        });
                    });
                }
            },
            function errorback(){

            }
        );
    };

//对工单进行投诉
//
    $scope.userComplain = function () {
            //判断是否投诉，如果已投诉，则无法再投诉，如果未投诉，才可以再投诉
        $http({
           method:'get',
            url:'/isComplaint/'+$scope.jobnumber
        }).then(
            function successback(data){
                console.log(data.data.data);
                if(data.data.data==null){
                    //如果未投诉，才可以再投诉
                    var r = confirm("您确定要投诉吗!");
                    if (r == true) {
                        alert("请耐心等待，系统将派人马上处理");}
                    $http({
                        method: 'post',
                        url: 'http://localhost:8080/complaint/' + $scope.jobnumber
                    }).then(function successCallback(response) {
                        $scope.complain = response.data.data;
                        console.log(response);

                    }, function errorCallback(response) {
                        jError("投诉失败，请重试！",{
                            VerticalPosition: 'center',
                            HorizontalPosition: 'center'
                        });
                    });
                }else{
                    var iscom = data.data.data.commentContent;
                    //如果已投诉，则无法再投诉
                    if(iscom=="投诉"){
                        angular.element(".theThrow").html("<p style='color:#000;margin-left: 20px;margin-top: 50px;'>已投诉，请勿重复点击</p>");
                    }
                }

            }
        );


    };


// 修改个人学生资料
    $scope.updateStudentInfo = function () {
        $http({
            method: 'post',
            url: 'http://119.29.0.217:8080/sctu_it_operation_platform/updateUserInfo',
            data: {
                "userAccount": $scope.user.userAccount,
                "userName": $scope.user.userName,
                "userPassword": $scope.user.userPassword,
                "userAddress": $scope.user.userAddress,
                "userPhone": $scope.user.userPhone,
                "userType": $scope.user.userType
            }
        }).then(function successCallback(response) {
            console.log(response);
            if (response.data.msg == "成功" && response.data.data != null) {
                $scope.user = response.data.data;
            } else {
                alert("操作失败");
            }
            jSuccess("修改信息成功！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });

        }, function errorCallback(response) {
            jError("修改信息失败！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });
        });
    };


// 得到服务电话
    $scope.getServicePhone = function () {

        $http({
            method: 'get',
            url: 'http://119.29.0.217:8080/sctu_it_operation_platform/webServiceInfo'

        }).then(function successCallback(response) {

            console.log(response);
            $scope.servicephone = response.data.data;
        }, function errorCallback(response) {
            // 请求失败执行代码
            jError("得到服务电话失败！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });
        });
    };

    $scope.getServicePhone();


//历史工单页面：查看历史工单
    $scope.lookHistoryWork = function () {

        $http({
            method: 'get',
            url: 'http://119.29.0.217:8080/sctu_it_operation_platform/userWorkOrders/' + $scope.user.userAccount,

        }).then(function successCallback(response) {
            $scope.historyWork = response.data.data;
            console.log(response);


        }, function errorCallback(response) {
            jError("获取历史工单失败！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });
        });


    };

//首页获取一级目录，也就是获取报修问题
    $scope.getQuestion = function () {

        $http({
            method: 'get',
            url: 'http://localhost:8080/OneLevelDirectories'

        }).then(function successCallback(response) {
            $scope.questions = response.data.data.data;
            console.log(response);
        }, function errorCallback(response) {
            jError("获取故障分类失败！",{
                VerticalPosition: 'center',
                HorizontalPosition: 'center'
            });
        });


    };
    $scope.getQuestion();

});

//判断是否为空的拦截器
app.filter('reverse', function () {
    return function (text) {

        var txt = null;
        if (text != null) {
            txt = text
        } else {
            txt = "未投诉"
        }
        return txt;
    }
});
